function selectColumns(editor, csv, header) {
    console.log(csv, "this is csv");
    var selectEditor = new $.fn.dataTable.Editor();
    var fields = editor.order();
    for (var i = 0; i < fields.length; i++) {
        var field = editor.field(fields[i]);

        selectEditor.add({
            label: field.label(),
            name: field.name(),
            type: 'select',
            options: header,
            def: header[i]
        });
    }

    selectEditor.create({
        title: 'CSV文件映射到的字段',
        buttons: '导入 ' + csv.length + '条记录',
        message: 'Select the CSV column you want to use the data from for each field.'
    });

    selectEditor.on('submitComplete', function (e, json, data, action) {
        // Use the host Editor instance to show a multi-row create form allowing the user to submit the data.
        editor.create(csv.length, {
            title: 'Confirm import',
            buttons: 'Submit',
            message: 'Click the <i>Submit</i> button to confirm the import of ' + csv.length + ' rows of data. Optionally, override the value for a field to set a common value by clicking on the field below.'
        });

        for (var i = 0; i < fields.length; i++) {
            var field = editor.field(fields[i]);
            var mapped = data[field.name()];
            console.log(field, mapped);

            for (var j = 0; j < csv.length; j++) {
                field.multiSet(j, csv[j][mapped]);
            }
        }
        console.log(data, "!!!!!!!!!!", field);
    });
}


var editor = new $.fn.dataTable.Editor({
    ajax: "/api/result/upload_result/",
    table: "#table_id",
    fields: [{
        label: "学号:",
        name: "stu_id"
    }, {
        label: "语文:",
        name: "chinese"
    }, {
        label: "数学:",
        name: "math"
    }, {
        label: "英语:",
        name: "english"
    }
    ]
});
var uploadEditor = new $.fn.dataTable.Editor({
    fields: [{
        label: 'CSV file:',
        name: 'csv',
        type: 'upload',
        ajax: function (files) {
            // Ajax override of the upload so we can handle the file locally. Here we use Papa
            // to parse the CSV.
            Papa.parse(files[0], {
                header: true,
                skipEmptyLines: true,
                complete: function (results) {
                    if (results.errors.length) {
                        console.log(results);
                        uploadEditor.field('csv').error('CSV parsing error: ' + results.errors[0].message);
                    } else {
                        uploadEditor.close();
                        selectColumns(editor, results.data, results.meta.fields);
                    }
                }
            });
        }
    }]
});
$('#table_id').DataTable({
    dom: 'Bftrlip',
    buttons: [
        {
            extend: "collection",
            text: "导出",
            buttons: ["excel", "pdf", "csv"]
        },
        {
            extend: "print",
            text: "打印"
        },
        {
            text: '导入 CSV',
            action: function () {
                uploadEditor.create({
                    title: 'CSV file import'
                });
            }
        }

    ],
    select: true,
    ajax: '/api/result/get_all_result/',
    columns: [
        {data: 'stu_id'},
        {data: 'chinese'},
        {data: 'math'},
        {data: 'english'}
    ]
});
